<?php

namespace App\Http\Controllers\Users;

use App\Http\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RegisterController extends Controller
{
    // 注册页面
    public function index()
    {
        return view('user.register');
    }

    // 注册行为
    public function register(Request $request)
    {
        // 验证
        $rules = [
            'name' => 'required|min:4|unique:users,name',
            'email' => 'required|min:4|unique:users,email|email',
            'password' => 'required|min:6|max:16|confirmed',
            'password_confirmation' => 'required|min:6|max:16',
        ];
        $this->validate($request, $rules);

        // 保存
        $name = $request->input('name');
        $email = $request->input('email');
        $password = bcrypt($request->input('password'));
        User::create(compact('name', 'email', 'password'));

        // 渲染
        return redirect('/user/login');
    }
}
